//
//  ITImageManager.m
//  InTubePro
//
//  Created by seven on 2020/4/9.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITImageManager.h"
@interface ITImageManager(){
    
}
IT_PROPERTY_ASSIGN int width;
@end

@implementation ITImageManager

- (instancetype)initWithWidth:(int)width{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                               NSUserDomainMask, YES) firstObject];
    cachePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"icon_%d",width]];
    NSOperationQueue *queue = [NSOperationQueue new];
    if ([queue respondsToSelector:@selector(setQualityOfService:)]) {
        queue.qualityOfService = NSQualityOfServiceBackground;
    }
    YYImageCache *cache = [[YYImageCache alloc] initWithPath:cachePath];
    self = [super initWithCache:cache queue:queue];
    if (self) {
        self.width = width;
        WEAKSELF
        self.sharedTransformBlock = ^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
            UIImage *icon = image;
            CGFloat scale = UIScreen.mainScreen.scale;
            CGFloat imageWidth = width * scale;
            icon = [image imageByResizeToSize:CGSizeMake(imageWidth, imageWidth) contentMode:(UIViewContentModeScaleAspectFill)];
            int border = scale/2;
            icon = [icon imageByRoundCornerRadius:imageWidth/2 borderWidth:border borderColor:ColorWithHex(0xBEBEBE)];
            return icon;
        };
    }
    
    return self;
}


+ (instancetype)managerWithWidth:(int)width{
    static NSMutableDictionary *_managerInfo = nil;
    if (_managerInfo == nil) {
        _managerInfo = NSMutableDictionary.new;
    }
    ITImageManager *_manager = _managerInfo[@(width)];
    if (_manager == nil) {
        _manager = [[ITImageManager alloc] initWithWidth:width];
    }
    return _manager;
}

@end
